package com.myzl.quartz.service.impl;

import cn.hutool.core.date.DatePattern;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.myzl.quartz.domain.SysJobLog;
import com.myzl.quartz.mapper.SysJobLogMapper;
import com.myzl.quartz.service.ISysJobLogService;
import com.myzl.system.domain.SysLogininfor;
import com.myzl.system.domain.SysOperLog;
import com.myzl.system.mapper.SysLogininforMapper;
import com.myzl.system.mapper.SysOperLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 定时任务调度日志信息 服务层
 *
 * @author myzl
 */
@Service
public class SysJobLogServiceImpl implements ISysJobLogService
{
    @Autowired
    private SysJobLogMapper jobLogMapper;
    @Autowired
    private SysLogininforMapper logininforMapper;
    @Autowired
    private SysOperLogMapper operLogMapper;

    /**
     * 获取quartz调度器日志的计划任务
     *
     * @param jobLog 调度日志信息
     * @return 调度任务日志集合
     */
    @Override
    public List<SysJobLog> selectJobLogList(SysJobLog jobLog)
    {
        return jobLogMapper.selectJobLogList(jobLog);
    }

    /**
     * 通过调度任务日志ID查询调度信息
     *
     * @param jobLogId 调度任务日志ID
     * @return 调度任务日志对象信息
     */
    @Override
    public SysJobLog selectJobLogById(Long jobLogId)
    {
        return jobLogMapper.selectJobLogById(jobLogId);
    }

    /**
     * 新增任务日志
     *
     * @param jobLog 调度日志信息
     */
    @Override
    public void addJobLog(SysJobLog jobLog)
    {
        jobLogMapper.insertJobLog(jobLog);
    }

    /**
     * 批量删除调度日志信息
     *
     * @param logIds 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJobLogByIds(Long[] logIds)
    {
        return jobLogMapper.deleteJobLogByIds(logIds);
    }

    /**
     * 删除任务日志
     *
     * @param jobId 调度日志ID
     */
    @Override
    public int deleteJobLogById(Long jobId)
    {
        return jobLogMapper.deleteJobLogById(jobId);
    }

    /**
     * 清空任务日志
     */
    @Override
    public void cleanJobLog()
    {
        jobLogMapper.cleanJobLog();
    }

    @Override
    public void clearLog() {
        Integer oneHundredThousand = 10000;
        // 减一个月，删除一个月之前的数据
        LocalDateTime now = LocalDateTime.now().minusDays(3);
        String time = now.format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));

        Integer count = jobLogMapper.selectCount(new LambdaQueryWrapper<>());
        if (count >= oneHundredThousand){
            jobLogMapper.delete(new LambdaUpdateWrapper<SysJobLog>().
                    le(SysJobLog::getCreateTime,time));
        }

        count = logininforMapper.selectCount(new LambdaQueryWrapper<>());
        if (count >= oneHundredThousand){
            logininforMapper.delete(new LambdaUpdateWrapper<SysLogininfor>().
                    le(SysLogininfor::getCreateTime,time));
        }

        count = operLogMapper.selectCount(new LambdaQueryWrapper<>());
        if (count >= oneHundredThousand){
            operLogMapper.delete(new LambdaUpdateWrapper<SysOperLog>().
                    le(SysOperLog::getCreateTime,time));
        }
    }

}
